package cn.tedu.user.controller;

import cn.tedu.user.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.jt.common.pojo.User;
import com.jt.common.utils.CookieUtils;
import com.jt.common.vo.SysResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/user/manage")
public class UserController {
    @Autowired
    private UserService userService;
    //用户名校验
    @RequestMapping("/checkUserName")
    @ResponseBody
    public SysResult checkUserName(String userName){
        //业务层校验后返回available
        boolean available=userService.checkUserName(userName);
        if (available){
            return SysResult.ok();
        }else {
            return SysResult.build(201,"不可用",null);
        }
    }

    //注册
    @RequestMapping("/save")
    @ResponseBody
    public SysResult doRegister(User user){
        try {
            userService.doRegister(user);
            return SysResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return SysResult.build(201,"注册失败",null);
        }
    }
    @RequestMapping("/send")
    public SysResult send(String phoneNum){

        userService.sendMs(phoneNum);
        return SysResult.ok();
    }
    //用户登录校验
    @RequestMapping("/login")
    @ResponseBody
    public SysResult doLogin(User user, HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException {
        String ticket = userService.doLogin(user);
        if (StringUtils.isEmpty(ticket)){
            return  SysResult.build(201,"登录失败",null);
        }else {
            CookieUtils.setCookie(request,response,"EM_TICKET",ticket);
            return SysResult.ok();
        }
    }
    //获取登录状态
    @RequestMapping("/query/{ticket}")
    @ResponseBody
    public SysResult queryUserjson(@PathVariable String ticket){
        String userJson=userService.queryUserjson(ticket);
        if (StringUtils.isEmpty(userJson)){
            return SysResult.build(201,"登录超时",null);
        }else{
            return SysResult.build(200,"正常",userJson);
        }
    }
    //登出
    @RequestMapping("/logout")
    @ResponseBody
    public SysResult logout(HttpServletRequest request, HttpServletResponse response){
        try {
            String ticket = CookieUtils.getCookieValue(request, "EM_TICKET");
            if (StringUtils.isNotEmpty(ticket)){
                userService.logout(ticket);
            }
            CookieUtils.deleteCookie(request,response,"EM_TICKET");
            return SysResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return SysResult.build(201,"失败",null);
        }
    }


    @RequestMapping("/github/login")
    public String githubLogin(){
        return userService.githubLogin();
    }
    @RequestMapping("/callback")
    @ResponseBody
    public String getTokenAndResource(String code,HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException {
        //System.out.println(code);
        String ticket = userService.getTokenAndResource(code);
        if (StringUtils.isEmpty(ticket)){
            return "登录失败了";
        }else {
            CookieUtils.setCookie(request,response,"EM_TICKET",ticket);
            return "<a href='http://www.easymall.com'>您已成功登录，请返回主页查看</a>";
        }
    }

}
